package dev.ece.suit.loader.models.fbx;

import java.util.List;
import java.util.Map;

public class FBXNodeAttribute {

	public final static String NODE_ATTRIBUTE = "NodeAttribute";
	
	private long id;
	
	private String type;
	
	private double size;
	
	public static FBXNodeAttribute[] parseFBXNodeAttribute(List<FBXElement> elements) {
		FBXNodeAttribute[] attrs = null;
		if(elements != null && !elements.isEmpty()) {
			attrs = new FBXNodeAttribute[elements.size()];
			FBXElement element = null;
			FBXElement properties = null;
			FBXElement typeflags = null;
			FBXElement size = null;
			for(int i=0; i<attrs.length; i++) {
				element = elements.get(i);
				attrs[i] = new FBXNodeAttribute();
				attrs[i].id = (Long) element.getProperties()[0].getValue();
				typeflags = element.findFirstChild(FBXModelLoader.TYPE_FLAGS);
				if(typeflags != null) {
					attrs[i].type = (String) typeflags.getProperties()[0].getValue();
				}
				properties = element.findFirstChild(FBXModelLoader.PROPERTIES70);
				if(properties != null) {
					size = properties.findFirstChild(FBXModelLoader.P);
					if(size != null) {
						attrs[i].size = (Double) size.getProperties()[4].getValue();
					}
				}
			}
		}
		return attrs;
	}

	public static void connectToFBXModel(Map<Long, List<FBXConnection>> connections, FBXNodeAttribute[] nodeAttributes, FBXModel[] models) {
		if(nodeAttributes != null && nodeAttributes.length > 0) {
			Map<Long, FBXModel> map = FBXModel.toFBXModelMap(models);
			List<FBXConnection> conns = null;
			FBXModel model = null;
			for(FBXNodeAttribute attr: nodeAttributes) {
				conns = connections.get(attr.getId());
				if(conns != null) {
					for(FBXConnection conn: conns) {
						model = map.get(conn.getTo());
						if(model != null) {
							model.setNodeAttribute(attr);
						}
					}
				}
			}
		}
	}
	
	public long getId() {
		return id;
	}

	public String getType() {
		return type;
	}

	public double getSize() {
		return size;
	}

	@Override
	public String toString() {
		return String.format("(id=%s,type:%s,size:%s)", this.id, this.type, this.size);
	}
	
}
